<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/46">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Apache/15. apache的两种工作模式详解/">apache的两种工作模式详解</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<h2 id="1、prefork-c模块-一个非线程型的、预派生的MPM"><a href="#1、prefork-c模块-一个非线程型的、预派生的MPM" class="headerlink" title="1、prefork.c模块(一个非线程型的、预派生的MPM)"></a>1、prefork.c模块(一个非线程型的、预派生的MPM)</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;prefork MPM 使用多个子进程，每个子进程只有一个线程。每个进程在某个确定的时间只能维持一个连接。在大多数平台上，Prefork MPM在效率上要比Worker MPM要高，但是内存使用大得多。prefork的无线程设计在某些情况下将比worker更有优势：他能够使用那些没有处理好线程安全的第三方模块，并 且对于那些线程调试困难的平台而言，他也更容易调试一些。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">ServerLimit   20000</div><div class="line">StartServers   5</div><div class="line">MinSpareServers   5</div><div class="line">MaxSpareServers   10</div><div class="line">MaxClients   1000</div><div class="line">MaxRequestsPerChild 0</div></pre></td></tr></table></figure>
<ul>
<li>ServerLimit  2000：默认的MaxClient最大是256个线程,假如想配置更大的值，就的加上ServerLimit这个参数。20000是ServerLimit这个参数的最大值。假如需要更大，则必须编译apache,此前都是无需重新编译Apache。<br>生效前提：必须放在其他指令的前面</li>
<li>StartServers   5：指定服务器启动时建立的子进程数量，prefork默认为5。</li>
<li>MinSpareServers   5：指定空闲子进程的最小数量，默认为5。假如当前空闲子进程数少于MinSpareServers ，那么Apache将以最大每秒一个的速度产生新的子进程。此参数不要设的太大。</li>
<li>MaxSpareServers   10：配置空闲子进程的最大数量，默认为10。假如当前有超过MaxSpareServers数量的空闲子进程，那么父进程将杀死多余的子进程。此参数不要设的太大。假如您将该指令的值配置为比MinSpareServers小，Apache将会自动将其修改成”MinSpareServers+1”。</li>
<li>MaxClients   256：限定同一时间客户端最大接入请求的数量，默认为256。任何超过MaxClients限制的请求都将进入等候队列,一旦一个链接被释放，队列中的请求将得到服务。要增大这个值，您必须同时增大ServerLimit 。</li>
<li>MaxRequestsPerChild 10000：每个子进程在其生存期内允许伺服的最大请求数量，默认为10000.到达MaxRequestsPerChild的限制后，子进程将会结束。假如MaxRequestsPerChild为”0”，子进程将永远不会结束。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将MaxRequestsPerChild配置成非零值有两个好处：</p>
<ol>
<li>能够防止(偶然的)内存泄漏无限进行，从而耗尽内存。 </li>
<li>给进程一个有限寿命，从而有助于当服务器负载减轻的时候减少活动进程的数量(重生的机会)。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>工作方式</strong>：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一个单独的控制进程(父进程)负责产生子进程，这些子进程用于监听请求并作出应答。Apache总是试图保持一些备用的 (spare)或是空闲的子进程 用于迎接即将到来的请求。这样客户端就无需在得到服务前等候子进程的产生。在Unix系统中，父进程通常以root身份运行以便邦定80端口，而 Apache产生的子进程通常以一个低特权的用户运行。User和Group指令用于配置子进程的低特权用户。运行子进程的用户必须要对他所服务的内容有 读取的权限，但是对服务内容之外的其他资源必须拥有尽可能少的权限。</p>
<h2 id="2、worker-c模块-支持混合的多线程多进程的多路处理模块"><a href="#2、worker-c模块-支持混合的多线程多进程的多路处理模块" class="headerlink" title="2、worker.c模块(支持混合的多线程多进程的多路处理模块)"></a>2、worker.c模块(支持混合的多线程多进程的多路处理模块)</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;worker MPM 使用多个子进程，每个子进程有多个线程。每个线程在某个确定的时间只能维持一个连接。通常来说，在一个高流量的HTTP服务器上，Worker MPM是个比较好的选择，因为Worker MPM的内存使用比Prefork MPM要低得多。但worker MPM也由不完善的地方，假如一个线程崩溃，整个进程就会连同其任何线程一起”死掉”.由于线程共享内存空间，所以一个程式在运行时必须被系统识别为”每个线程都是安全的”。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">ServerLimit   50</div><div class="line">ThreadLimit   200</div><div class="line">StartServers   5</div><div class="line">MaxClients   5000</div><div class="line">MinSpareThreads   25</div><div class="line">MaxSpareThreads   500</div><div class="line">ThreadsPerChild   100</div><div class="line">MaxRequestsPerChild 0</div></pre></td></tr></table></figure>
<ul>
<li>ServerLimit   16：服务器允许配置的进程数上限。这个指令和ThreadLimit结合使用配置了MaxClients最大允许配置的数值。任何在重启期间对这个指令的改变都将被忽略，但对MaxClients的修改却会生效。</li>
<li>ThreadLimit 64：每个子进程可配置的线程数上限。这个指令配置了每个子进程可配置的线程数ThreadsPerChild上限。任何在重启期间对这个指令的改变都将被忽略，但对ThreadsPerChild的修改却会生效。默认值是”64”.</li>
<li>StartServers 3：服务器启动时建立的子进程数，默认值是”3”。</li>
<li>MinSpareThreads 75：最小空闲线程数,默认值是”75”。这个MPM将基于整个服务器监控空闲线程数。假如服务器中总的空闲线程数太少，子进程将产生新的空闲线程。</li>
<li>MaxSpareThreads 250：配置最大空闲线程数。默认值是”250”。这个MPM将基于整个服务器监控空闲线程数。假如服务器中总的空闲线程数太多，子进程将杀死多余的空闲线 程。MaxSpareThreads的取值范围是有限制的。Apache将按照如下限制自动修正您配置的值：worker需要其大于等于 MinSpareThreads加上ThreadsPerChild的和</li>
<li>MaxClients 400：允许同时伺服的最大接入请求数量(最大线程数量)。任何超过MaxClients限制的请求都将进入等候 队列。默认值是”400”,16 (ServerLimit)乘以25(ThreadsPerChild)的结果。因此要增加MaxClients的时候，您必须同时增加 ServerLimit的值。</li>
<li>ThreadsPerChild 25：每个子进程建立的常驻的执行线程数。默认值是25。子进程在启动时建立这些线程后就不再建立新的线程了。</li>
<li>MaxRequestsPerChild 0：配置每个子进程在其生存期内允许伺服的最大请求数量。到达MaxRequestsPerChild的限制后，子进程将会结束。假如MaxRequestsPerChild为”0”，子进程将永远不会结束。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将MaxRequestsPerChild配置成非零值有两个好处：</p>
<ol>
<li>能够防止(偶然的)内存泄漏无限进行，从而耗尽内存。 </li>
<li>给进程一个有限寿命，从而有助于当服务器负载减轻的时候减少活动进程的数量(重生的机会)。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：对于KeepAlive链接，只有第一个请求会被计数。事实上，他改变了每个子进程限制最大链接数量的行为。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;工作方式：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;每个进程能够拥有的线程数量是固定的。服务器会根据负载情况增加或减少进程数量。一个单独的控制进程(父进程)负责子进程的建 立。每个子进程能够建立 ThreadsPerChild数量的服务线程和一个监听线程，该监听线程监听接入请求并将其传递给服务线程处理和应答。Apache总是试图维持一个备 用(spare)或是空闲的服务线程池。这样，客户端无须等待新线程或新进程的建立即可得到处理。在Unix中，为了能够绑定80端口，父进程一般都是以 root身份启动，随后，Apache以较低权限的用户建立子进程和线程。User和Group指令用于配置Apache子进程的权限。虽然子进程必须对 其提供的内容拥有读权限，但应该尽可能给予他较少的特权。另外，除非使用了suexec ，否则，这些指令配置的权限将被CGI脚本所继承。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;硬限制：</p>
<ul>
<li>ServerLimi和ThreadLimit这两个指令决定了活动子进程数量和每个子进程中线程数量的硬限制。要想改变这个硬限制必须完全停止服务器然后再启动服务器(直接重启是不行的)。</li>
<li>Apache在编译ServerLimit时内部有一个硬性的限制，您不能超越这个限制。</li>
<li>prefork MPM最大为”ServerLimit 200000”</li>
<li>其他MPM(包括work MPM)最大为”ServerLimit 20000</li>
<li>Apache在编译ThreadLimit时内部有一个硬性的限制，您不能超越这个限制。</li>
<li>mpm_winnt是”ThreadLimit 15000”</li>
<li>其他MPM(包括work prefork)为”ThreadLimit 20000</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：使用ServerLimit和ThreadLimit时要特别当心。假如将ServerLimit和ThreadLimit配置成一个高出实际需要许多的值，将会有过多的共享内存被分配。当配置成超过系统的处理能力，Apache可能无法启动，或系统将变得不稳定。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/14. apache的扩展模块安装/">apache的扩展模块安装</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个用到了apache的扩展工具  apxs</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在使用这个功能之前，请先确认是否已经加载了 mod_so 模块，方法是：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/usr/<span class="built_in">local</span>/apache2/bin/httpd -l</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在列出的内容中，如果有 mod_so 那么说明已经加载了该模块。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面，举个例子来演示一下，如何编译安装扩展模块，例如，我想增加  mod_status.so 这个模块</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/usr/<span class="built_in">local</span>/apache2/bin/apxs -i -a -c mod_status.c</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;请注意，这里的mod_status.c 的路径一定要写对了，不然会报错，一般情况下，这个C文件在你的源码包下，比如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/usr/<span class="built_in">local</span>/src/httpd-2.0.59/modules/generators/mod_status.c</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编译的结果类似这样：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">/usr/<span class="built_in">local</span>/services/apache-2.0.59/build/libtool --silent --mode=compile gcc -prefer-pic -DAP_HAVE_DESIGNATED_INITIALIZER -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -g -O2 -pthread -I/usr/<span class="built_in">local</span>/services/apache-2.0.59/include -I/usr/<span class="built_in">local</span>/services/apache-2.0.59/include -I/usr/<span class="built_in">local</span>/services/apache-2.0.59/include -c -o /root/httpd-2.0.64/modules/generators/mod_status.lo /root/httpd-2.0.64/modules/generators/mod_status.c &amp;&amp; touch /root/httpd-2.0.64/modules/generators/mod_status.slo</div><div class="line">/usr/<span class="built_in">local</span>/services/apache-2.0.59/build/libtool --silent --mode=link gcc -o /root/httpd-2.0.64/modules/generators/mod_status.la -rpath /usr/<span class="built_in">local</span>/services/apache-2.0.59/modules -module -avoid-version /root/httpd-2.0.64/modules/generators/mod_status.lo</div><div class="line">/usr/<span class="built_in">local</span>/services/apache-2.0.59/build/instdso.sh SH_LIBTOOL=<span class="string">'/usr/local/services/apache-2.0.59/build/libtool'</span> /root/httpd-2.0.64/modules/generators/mod_status.la /usr/<span class="built_in">local</span>/services/apache-2.0.59/modules</div><div class="line">/usr/<span class="built_in">local</span>/services/apache-2.0.59/build/libtool --mode=install cp /root/httpd-2.0.64/modules/generators/mod_status.la /usr/<span class="built_in">local</span>/services/apache-2.0.59/modules/</div><div class="line">cp /root/httpd-2.0.64/modules/generators/.libs/mod_status.so /usr/<span class="built_in">local</span>/services/apache-2.0.59/modules/mod_status.so</div><div class="line">cp /root/httpd-2.0.64/modules/generators/.libs/mod_status.lai /usr/<span class="built_in">local</span>/services/apache-2.0.59/modules/mod_status.la</div><div class="line">cp /root/httpd-2.0.64/modules/generators/.libs/mod_status.a /usr/<span class="built_in">local</span>/services/apache-2.0.59/modules/mod_status.a</div><div class="line">ranlib /usr/<span class="built_in">local</span>/services/apache-2.0.59/modules/mod_status.a</div><div class="line">chmod 644 /usr/<span class="built_in">local</span>/services/apache-2.0.59/modules/mod_status.a</div><div class="line">PATH=<span class="string">"<span class="variable">$PATH</span>:/sbin"</span> ldconfig -n /usr/<span class="built_in">local</span>/services/apache-2.0.59/modules</div><div class="line">----------------------------------------------------------------------</div><div class="line">Libraries have been installed <span class="keyword">in</span>:</div><div class="line"> /usr/<span class="built_in">local</span>/services/apache-2.0.59/modules</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;完成后，请看提示 Libraries have been installed in:  模块就会安装到这里了。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/13. apache的server status 功能/">apache的server status 功能</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先要查看，你的apache是否有相应的模块</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ls  /usr/<span class="built_in">local</span>/apache2/modules/mod_status.so</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果没有，请重新编译你的apache</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加上 –enable-module=so , 然后再安装一下扩展模块 mod_status.so </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果你使用的是rpm包安装的apache，就无需做这一步了，因为rpm安装的是有该模块的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;接下来，就需要配置你的apache了。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim  http.conf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">LoadModule status_module      modules/mod_status.so</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还需要增加：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">ExtendedStatus On</div><div class="line"></div><div class="line"></div><div class="line">SetHandler Server-status</div><div class="line">Order deny,allow</div><div class="line">Deny from all</div><div class="line">Allow from 192.168.111.36</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为了安全期间，一定要做一个allow deny的规则。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启apache就可以使用这个功能了，查看方法：<a href="http://www.example.com/Server-status" target="_blank" rel="external">http://www.example.com/Server-status</a></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/12. apache 的动态和静态/">apache 的动态和静态</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>关于apache的动态与静态编译的理解</strong>，引用某人的比喻如下：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;好比有两个人a和m，a代表apache,m代表module，要想让a使用m的东西，一个方法是把m的东西都放到a那里去，a使用的时候就是现成的了，就是所谓的静态编译</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还有一个方法，就是告诉a，m的住址，当a要使用m的东西的时候,a去找m,然后使用，不过，这种方法要注意的一个问题就是：m必须要有实际的住址，否则a会找不到m而产生错误的，这种方法也就是apache 的动态(DSO)编译了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>下面说说在apache1.3和apache2.0动态与静态编译编译的区别</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先是<strong>apache1.3.29</strong>+php4.3.4+mysql4.0.13的<strong>静态编译</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;apache第一次编译,并不要安装，因为php的编译需要apache至少已经编译过一次 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">tar zvxf apache_1.3.27.tar.gz </div><div class="line"><span class="built_in">cd</span> apache_1.3.27 </div><div class="line">./configure --prefix=/usr/<span class="built_in">local</span>/apache</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编译php</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">tar zvxf php4.3.4.tar.gz </div><div class="line"><span class="built_in">cd</span> php4.3.4</div><div class="line">./configure --prefix=/usr/<span class="built_in">local</span>/php \</div><div class="line">--with-mysql=/usr/<span class="built_in">local</span>/mysql \</div><div class="line">--with-apache=../apache_1.3.27 </div><div class="line">make</div><div class="line">make install</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第二次编译安装apache: </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">cd</span> ../apache_1.3.29</div><div class="line">./configure --prefi=/usr/<span class="built_in">local</span>/apache \</div><div class="line">--activate-module=src/modules/php4/libphp4.a </div><div class="line">make </div><div class="line">make install</div><div class="line">cp ../php4.3.4/php.ini.dist /usr/<span class="built_in">local</span>/php/lib/php.ini</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改/usr/local/apache/conf/httpd.conf</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查找<ifmodule mod_mime.c=""></ifmodule></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在此范围添加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">AddType application/x-httpd-php .php </div><div class="line">AddType application/x-httpd-php-source .phps</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：apache和php的源码包在同一个目录，–with-apache=../apache_1.3.27是指向源码解压的目录</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ok!静态编译完成。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再看<strong>DSO动态编译</strong>方法：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先编译安装apache</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">tar zvxf apache_1.3.29</div><div class="line"><span class="built_in">cd</span> apache_1.3.29</div><div class="line">./configure --prefix=/usr/<span class="built_in">local</span>/apache \</div><div class="line">--<span class="built_in">enable</span>-module=so \</div><div class="line">--<span class="built_in">enable</span>-module=rewrite \</div><div class="line">--<span class="built_in">enable</span>-shared=max</div><div class="line">make </div><div class="line">make install</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;so模块用来提供DSO支持的apachehe核心模块，rewrite是地址重写的模块，如果不需要可以不编译enable－shared＝max是指除了so以外的所有标准模块都编译成DSO模块。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后编译php</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">tar zvxf php4.3.4.tar.gz </div><div class="line"><span class="built_in">cd</span> php4.3.2 </div><div class="line">./configure --prefix=/usr/<span class="built_in">local</span>/php \</div><div class="line">--with-mysql=/usr/<span class="built_in">local</span>/mysql \</div><div class="line">--with-apxs=/usr/<span class="built_in">local</span>/apache/bin/apxs </div><div class="line">make</div><div class="line">make install</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后修改httpd.conf，方法同静态编译的方法</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;OK，DSO动态编译就完成了！</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;接下来我们讲<strong>apache2.0.46</strong>+php4.3.2的<strong>动态编译</strong>方法</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一样先编译安装apache</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">tar zvxf httpd-2.0.46.tar.gz</div><div class="line"><span class="built_in">cd</span> httpd-2.0.46</div><div class="line">./configure --prefix=/usr/<span class="built_in">local</span>/apache2 \</div><div class="line">--<span class="built_in">enable</span>-so \</div><div class="line">--<span class="built_in">enable</span>-mods-shared=most</div><div class="line">make</div><div class="line">make install</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>大家注意了–enable-so相当与1.3.27的–enable-module=so，而–enable-mods-shared=most又等同与以前的–enable-shared=max</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后还是编译PHP</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">tar zvxf php4.3.2.tar.gz</div><div class="line">./configure --prefix=/usr/<span class="built_in">local</span>/php \</div><div class="line">--with-mysql=/usr/<span class="built_in">local</span>/mysql \</div><div class="line">--with-apxs2=/usr/<span class="built_in">local</span>/apache2/bin/apxs </div><div class="line">make </div><div class="line">make install</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>注意这里是apxs2！！</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改httpd.conf与1.3.27也有所不同，大家寻找Add Type application/x-tar .tgz 在下面添加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">AddType application/x-httpd-php .php </div><div class="line">AddType application/x-httpd-php-source .phps</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另外要显示中文的话，请修改:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">AddDefaultCharset gb2312</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/11. apache mod_proxy 扩展模块安装/">apache mod_proxy 扩展模块安装</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;现在用到apache的扩展功能了，可是在第一次编译的时候，没有编译进这个模块去？怎么办呢，可以通过扩展的方式编译一下。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载相同版本的apache源代码，注意，一定是要相同版本的，否则编译不能成功。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载后，解压</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">cd</span>  httpd-2.0.59/modules/proxy</div><div class="line">/usr/<span class="built_in">local</span>/apache2/bin/apxs -c -i -a mod_proxy.c proxy_connect.c proxy_http.c proxy_util.c</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从输出里面看到apache的modules目录下已经产生了mod_proxy.so,且已经在httpd.conf中激活了</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/apache2/conf/</div><div class="line">ls ../modules/</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看到确实有mod_prxoy.so</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim httpd.conf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改如下</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加载模块</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">LoadModule proxy_module modules/mod_proxy.so (这句是编译激活时产生的)</div><div class="line">LoadModule proxy_http_module modules/mod_proxy.so (这句是要手动添加的)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;到这里proxy模块的支持算是已经完成了，下面就该配置虚拟主机了。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/10. Apache模块动态加载和静态加载/">Apache模块动态加载和静态加载</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以前对apache研究的不够深刻，甚至连模块的动态以及静态都搞不清楚。查了资料，基本上明了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;静态，顾名思义就是apache本身不用干什么，不用动就可以使用模块。这就需要把模块都编译进apache的httpd文件中。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;动态，需要apache去调用这个模块，用哪个调用哪个，这种方式的前提是，需要告诉apache这些模块在哪里。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;现在apache版本都普遍都是2.0或者2.2了，针对apache2.x如何动态以及静态编译?</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于2.x版本来讲，编译apache的时候，只要加上  –enable-mods-shared=all 就可以动态的编译全部模块。安装完成后会在  apache2/modules 目录下多了很多模块文件。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果想静态加载所有模块，编译参数改成  –enable-mods=all</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Apache/1. Apache编译安装/">Apache编译安装</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Apache也需要到官网下载合适的版本，目前使用较多的版本为2.0或者2.2建议下载2.2版本。<a href="http://www.apache.org/dyn/closer.cgi" target="_blank" rel="external">Apache官网下载地址</a> 实际上，我们所谓的 apache ，真正的名字叫 httpd 。</p>
<h2 id="1-下载"><a href="#1-下载" class="headerlink" title="1.下载"></a>1.下载</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost mysql]<span class="comment"># cd /usr/local/src/</span></div><div class="line">[root@localhost src]<span class="comment"># wget http://syslab.comsenz.com/downloads/linux/httpd-2.2.16.tar.gz</span></div></pre></td></tr></table></figure>
<h2 id="2-解压"><a href="#2-解压" class="headerlink" title="2.解压"></a>2.解压</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost src]<span class="comment"># tar zxvf httpd-2.2.16.tar.gz</span></div></pre></td></tr></table></figure>
<h2 id="3-配置编译参数"><a href="#3-配置编译参数" class="headerlink" title="3.配置编译参数"></a>3.配置编译参数</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">[root@localhost src]<span class="comment"># cd httpd-2.2.16</span></div><div class="line">[root@localhost httpd-2.2.16]<span class="comment"># ./configure \</span></div><div class="line">--prefix=/usr/<span class="built_in">local</span>/apache2 \</div><div class="line">--with-included-apr \</div><div class="line">--<span class="built_in">enable</span>-so \</div><div class="line">--<span class="built_in">enable</span>-deflate=shared \</div><div class="line">--<span class="built_in">enable</span>-expires=shared \</div><div class="line">--<span class="built_in">enable</span>-rewrite=shared \</div><div class="line">--with-pcre</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>--prefix</code> 指定安装到哪里， <code>--enable-so</code> 表示启用DSO 。DSO是Dynamic Shared Objects（动态共享目标）的缩写，它提供了一种在运行时将特殊格式的代码在程序运行需要时，将需要的部分从外存调入内存执行的方法。Apache 支持动态共享模块，也支持静态模块，静态的话，会把需要的目标直接编译进apache的可执行文件中，相比较动态，虽然省去了加载共享模块的步骤，但是也加大了二进制执行文件的空间，变得臃肿。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>--enable-deflate=shared</code> 表示共享的方式编译deflate，后面的参数同理。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;那什么是动态共享，什么是静态？ apache 编译安装完成后会生成一个核心的二进制可执行文件叫做 httpd ，这个文件座位核心文件，提供服务时就是它在处理用户的请求，但是有一些功能，比如这里提到的 expires 就是配置静态文件（图片）过期时间的，也就是说图片可以在用户浏览器的临时缓存目录中缓存多久。这些功能是作为 httpd 的一个扩展模块来工作的，那么这种扩展模块有两种存在的方式，一种是直接在编译的时候和 httpd 文件拧到一起，组成一个体积大的文件，这种叫做静态。而另一种方式是，扩展模块作为一个独立的文件存在，只有在使用这个模块时再去调用它，这种叫做动态共享。两种方式中，动态的好处是，核心文件 httpd 比较小，模块随时用随时加载，耗费内存相对较少。而静态的优势是，在服务启动时，会把所有模块加载，用到时很快就执行，效率较高。建议倾向用动态。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果出现了这样的错误:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">error: mod_deflate has been requested but can not be built due to prerequisite failures</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决办法：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y zlib-devel</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为了避免在make的时候出现错误，所以最好是提前先安装好一些库文件:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y pcre pcre-devel apr apr-devel</div></pre></td></tr></table></figure>
<h2 id="4-编译"><a href="#4-编译" class="headerlink" title="4.编译"></a>4.编译</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost httpd-2.2.16]<span class="comment"># make</span></div></pre></td></tr></table></figure>
<h2 id="5-安装"><a href="#5-安装" class="headerlink" title="5.安装"></a>5.安装</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost httpd-2.2.16]<span class="comment"># make install</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上两个步骤都可以使用 <code>echo $?</code> 来检查是否正确执行，否则需要根据错误提示去解决问题。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost httpd-2.2.16]<span class="comment"># echo $?</span></div><div class="line">[root@localhost httpd-2.2.16]<span class="comment"># 0</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为 0 则没有问题。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Ansible/2. ansible判断 出现FATAL all hosts have already failed -- aborting 错误/">ansible判断 出现FATAL all hosts have already failed -- aborting 错误</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Ansible/">Ansible</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;今晚做ansible实验，验证到when判断ip的时候，遇到了个问题，就是使用ansible 127.0.0.1 -m setup 显示出来的内容并不像视频里面所显示的那样，每个值都以facter_开头的；也更找不到facter_ipaddress这个值，就是说使用facts ip判断条件是不能使用的？效果如下截图：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/ansible%E5%88%A4%E6%96%AD%20%E5%87%BA%E7%8E%B0FATAL%20all%20hosts%20have%20already%20failed%20--%20aborting%20%E9%94%99%E8%AF%AF/01.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后去谷歌搜索了下，发现也不少人也遇到这个问题得不到解决。看了那些大神回复根本看不懂，还有个使用Python把值提取出来的，但是不懂Python，好无奈。。。。只能自己想办法了。   </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最初试验的代码：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- hosts: testhosts</div><div class="line">  remote_user: root</div><div class="line">  gather_facts: True</div><div class="line">  tasks:</div><div class="line">    - shell: touch /tmp/when.txt</div><div class="line">      when: facter_ipaddress==<span class="string">"192.168.0.82"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决方案代码：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- hosts: testhosts</div><div class="line">  remote_user: root</div><div class="line">  gather_facts: True</div><div class="line">  tasks:</div><div class="line">    - shell: touch /tmp/when.txt</div><div class="line">      when: ansible_eth0.ipv4.address==<span class="string">"192.168.0.82"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;只是 facter_ipaddress变成了ansible_eth0.ipv4.address；至于为什么这样变？ ansible X.X.X.X -m setup 探索一下就知道了。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/ansible%E5%88%A4%E6%96%AD%20%E5%87%BA%E7%8E%B0FATAL%20all%20hosts%20have%20already%20failed%20--%20aborting%20%E9%94%99%E8%AF%AF/02.png?raw=true" alt=""></figure></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Ansible/1. Ansible 安装与配置/">Ansible 安装与配置</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Ansible/">Ansible</a>
			</span>
		
	</div>

	

	
		<h2 id="1-ansible特点"><a href="#1-ansible特点" class="headerlink" title="1. ansible特点"></a>1. ansible特点</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;不需要安装客户端，通过sshd去通信</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;基于模块工作，模块可以由任何语言开发</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;不仅支持命令行使用模块，也支持编写yaml格式的playbook</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;支持sudo</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有提供UI（浏览器图形化）www.ansible.com/tower  10台主机以内免费</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;开源UI  <a href="https://github.com/alaxli/ansible_ui" target="_blank" rel="external">https://github.com/alaxli/ansible_ui</a> </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;文档 <a href="http://download.csdn.net/detail/liyang23456/7741185" target="_blank" rel="external">http://download.csdn.net/detail/liyang23456/7741185</a></p>
<h2 id="2-ansible-安装"><a href="#2-ansible-安装" class="headerlink" title="2.ansible 安装"></a>2.ansible 安装</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;两台机器 192.168.0.82    192.168.0.81</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;只需要在192.168.0.82上安装ansible即可</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># yum install -y epel-release</span></div><div class="line">[root@ansible ~]<span class="comment"># yum install -y ansible</span></div></pre></td></tr></table></figure>
<h3 id="ansible-配置密钥"><a href="#ansible-配置密钥" class="headerlink" title="ansible 配置密钥"></a>ansible 配置密钥</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.82上生成密钥对</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ssh-keygen -t rsa  直接回车即可，不用设置密钥密码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ssh-keygen -t rsa</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把公钥(id_rsa.pub）内容放到对方机器（111）的/root/.ssh/authorized_keys里面</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># scp .ssh/id_rsa.pub 192.168.0.81:/root/.ssh/authorized_keys</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/01.png?raw=true" alt="01"><figcaption class="figure__caption">01</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本机也要操作 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># cat /root/.ssh/id_rsa.pub &gt;&gt; /root/.ssh/authorized_keys</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># chmod 600 /root/.ssh/authorized_keys</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关闭selinux</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/02.png?raw=true" alt="02"><figcaption class="figure__caption">02</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;检测连接</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># yum install -y openssh-clients</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ssh 192.168.0.81</span></div><div class="line">Nasty PTR record <span class="string">"192.168.0.81"</span> is <span class="built_in">set</span> up <span class="keyword">for</span> 192.168.0.81, ignoring</div><div class="line">Last login: Thu Feb 23 22:34:20 2017 from 192.168.0.100</div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/03.png?raw=true" alt="03"><figcaption class="figure__caption">03</figcaption></figure></p>
<h3 id="ansible-更改配置文件"><a href="#ansible-更改配置文件" class="headerlink" title="ansible 更改配置文件"></a>ansible 更改配置文件</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/hosts</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[testhost]</div><div class="line">127.0.0.1</div><div class="line">192.168.0.81</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/04.png?raw=true" alt="04"><figcaption class="figure__caption">04</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明： testhost为主机组名字，自定义的。 下面两个ip为组内的机器ip。</p>
<h2 id="3-Ansible-远程执行命令"><a href="#3-Ansible-远程执行命令" class="headerlink" title="3.Ansible 远程执行命令"></a>3.Ansible 远程执行命令</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m command -a 'w'</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/05.png?raw=true" alt="05"><figcaption class="figure__caption">05</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样就可以批量执行命令了。这里的testhost 为主机组名，-m后边是模块名字，-a后面是命令。当然我们也可以直接写一个ip，针对某一台机器来执行命令。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible 127.0.0.1 -m command -a 'hostname'</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/06.png?raw=true" alt="06"><figcaption class="figure__caption">06</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;错误： “msg”: “Aborting, target uses selinux but python bindings (libselinux-python) aren’t installed!”</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决： </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># yum install -y libselinux-python</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还有一个模块就是shell同样也可以实现</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m shell -a 'w'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;shell 和 command 的区别</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/07.png?raw=true" alt="07"><figcaption class="figure__caption">07</figcaption></figure></p>
<h2 id="4-ansible-拷贝文件或者目录"><a href="#4-ansible-拷贝文件或者目录" class="headerlink" title="4.ansible 拷贝文件或者目录"></a>4.ansible 拷贝文件或者目录</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mode=0644etest owner=root group=root mode=0755"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/08.png?raw=true" alt="08"><figcaption class="figure__caption">08</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：源目录会放到目标目录下面去，如果目标指定的目录不存在，它会自动创建。如果拷贝的是文件，dest指定的名字和源如果不同，并且它不是已经存在的目录，相当于拷贝过去后又重命名。但相反，如果desc是目标机器上已经存在的目录，则会直接把文件拷贝到该目录下面。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m copy -a "src=/etc/passwd dest=/tmp/123"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里的/tmp/123和源机器上的/etc/passwd是一致的，但如果目标机器上已经有/tmp/123目录，则会再/tmp/123目录下面建立passwd文件.如果没有123目录，则直接创建123文件。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/09.png?raw=true" alt="09"><figcaption class="figure__caption">09</figcaption></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/10.png?raw=true" alt="10"><figcaption class="figure__caption">10</figcaption></figure></p>
<h2 id="5-ansible-远程执行脚本"><a href="#5-ansible-远程执行脚本" class="headerlink" title="5.ansible 远程执行脚本"></a>5.ansible 远程执行脚本</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先创建一个shell脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /tmp/test.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">d=`date`</div><div class="line"><span class="built_in">echo</span> <span class="variable">$d</span> &gt; /tmp/an_test.txt</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/11.png?raw=true" alt="11"><figcaption class="figure__caption">11</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后把该脚本分发到各个机器上</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m copy -a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后是批量执行该shell脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m shell -a "/tmp/test.sh"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/12.png?raw=true" alt="12"><figcaption class="figure__caption">12</figcaption></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/13.png?raw=true" alt="13"><figcaption class="figure__caption">13</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;shell模块，还支持远程执行命令并且带管道</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m shell -a "cat /etc/passwd | wc -l"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/14.png?raw=true" alt="14"><figcaption class="figure__caption">14</figcaption></figure></p>
<h2 id="6-ansible-实现任务计划"><a href="#6-ansible-实现任务计划" class="headerlink" title="6.ansible 实现任务计划"></a>6.ansible 实现任务计划</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m cron -a "name='test cron' job='/bin/touch /tmp/1212.txt' weekday=6"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/15.png?raw=true" alt="15"><figcaption class="figure__caption">15</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;若要删除该cron 只需要加一个字段 state=absent</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m cron -a "name='test cron' state=absent"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/16.png?raw=true" alt="16"><figcaption class="figure__caption">16</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其他的时间表示：分钟 minute 小时 hour 日期 day 月份 month</p>
<h2 id="7-Ansible安装rpm包管理服务"><a href="#7-Ansible安装rpm包管理服务" class="headerlink" title="7.Ansible安装rpm包管理服务"></a>7.Ansible安装rpm包管理服务</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m yum -a "name=httpd"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/17.png?raw=true" alt="17"><figcaption class="figure__caption">17</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在name后面还可以加上state=installed</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible testhost -m service -a "name=httpd state=started enabled=yes"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/18.png?raw=true" alt="18"><figcaption class="figure__caption">18</figcaption></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/19.png?raw=true" alt="19"><figcaption class="figure__caption">19</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里的name是centos系统里的服务名，可以通过chkconfig –list查到。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Ansible文档的使用</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ansible-doc -l   列出所有的模块</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/20.png?raw=true" alt="20"><figcaption class="figure__caption">20</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ansible-doc cron  查看指定模块的文档</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/21.png?raw=true" alt="21"><figcaption class="figure__caption">21</figcaption></figure></p>
<h2 id="8-ansible-playbook-的使用"><a href="#8-ansible-playbook-的使用" class="headerlink" title="8.ansible playbook 的使用"></a>8.ansible playbook 的使用</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;相当于把模块写入到配置文件里面，例：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/test.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- hosts: testhost</div><div class="line">  remote_user: root</div><div class="line">  tasks:</div><div class="line">    - name: test_playbook</div><div class="line">    shell: touch /tmp/yanyi.txt</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/22.png?raw=true" alt="22"><figcaption class="figure__caption">22</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明： hosts参数指定了对哪些主机进行参作；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;user参数指定了使用什么用户登录远程主机操作；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;tasks指定了一个任务，其下面的name参数同样是对任务的描述，在执行过程中会打印出来。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible-playbook /etc/ansible/test.yml</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/23.png?raw=true" alt="23"><figcaption class="figure__caption">23</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.82</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/24.png?raw=true" alt="24"><figcaption class="figure__caption">24</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.81</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/25.png?raw=true" alt="25"><figcaption class="figure__caption">25</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再来一个创建用户的例子：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/create_user.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- name: create_user</div><div class="line">  hosts: testhost</div><div class="line">  user: root</div><div class="line">  gather_facts: <span class="literal">false</span></div><div class="line">  vars:</div><div class="line">    - user: <span class="string">"test"</span></div><div class="line">  tasks:</div><div class="line">    - name: create user</div><div class="line">    user: name=<span class="string">"&#123;&#123; user &#125;&#125;"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/26.png?raw=true" alt="26"><figcaption class="figure__caption">26</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明： name参数对该playbook实现的功能做一个概述，后面执行过程中，会打印 name变量的值 ，可以省略；gather_facts参数指定了在以下任务部分执行前，是否先执行setup模块获取主机相关信息，这在后面的task会使用到setup获取的信息时用到；vars参数，指定了变量，这里指字一个user变量，其值为test ，需要注意的是，变量值一定要用引号引住；user提定了调用user模块，name是user模块里的一个参数，而增加的用户名字调用了上面user变量的值。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible-playbook /etc/ansible/create_user.yml</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/27.png?raw=true" alt="27"><figcaption class="figure__caption">27</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.82</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/28.png?raw=true" alt="28"><figcaption class="figure__caption">28</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.81</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/29.png?raw=true" alt="29"><figcaption class="figure__caption">29</figcaption></figure></p>
<h2 id="9-ansible-playbook-中的循环"><a href="#9-ansible-playbook-中的循环" class="headerlink" title="9.ansible playbook 中的循环"></a>9.ansible playbook 中的循环</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/loop.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- hosts: testhost</div><div class="line">  user: root</div><div class="line">  tasks:</div><div class="line">    - name: change mode <span class="keyword">for</span> files</div><div class="line">      file: path=/tmp/&#123;&#123; item &#125;&#125; mode=600</div><div class="line">      with_items:</div><div class="line">        - 1.txt</div><div class="line">        - 2.txt</div><div class="line">        - 3.txt</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/30.png?raw=true" alt="30"><figcaption class="figure__caption">30</figcaption></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible-playbook /etc/ansible/loop.yml</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/31.png?raw=true" alt="31"><figcaption class="figure__caption">31</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.82 文件权限改为600</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/32.png?raw=true" alt="32"><figcaption class="figure__caption">32</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;192.168.0.81 文件权限改为600</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/33.png?raw=true" alt="33"><figcaption class="figure__caption">33</figcaption></figure></p>
<h2 id="10-ansible-playbook-条件判断"><a href="#10-ansible-playbook-条件判断" class="headerlink" title="10.ansible playbook 条件判断"></a>10.ansible playbook 条件判断</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/when.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- hosts: testhost</div><div class="line">  user: root</div><div class="line">  gather_facts: True</div><div class="line">  tasks:</div><div class="line">    - name: use when</div><div class="line">      shell: touch /tmp/when.txt</div><div class="line">      when: ansible_eth0.ipv4.address == <span class="string">"192.168.0.82"</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/34.png?raw=true" alt="34"><figcaption class="figure__caption">34</figcaption></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible-playbook /etc/ansible/when.yml</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/35.png?raw=true" alt="35"><figcaption class="figure__caption">35</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<a href="https://hcldirgit.github.io/2017/08/20/Ansible/2.%20ansible%E5%88%A4%E6%96%AD%20%E5%87%BA%E7%8E%B0FATAL%20all%20hosts%20have%20already%20failed%20--%20aborting%20%E9%94%99%E8%AF%AF/" target="_blank" rel="external">ansible判断 出现FATAL all hosts have already failed – aborting 错误</a></p>
<h2 id="11-ansible-playbook-中的-handlers"><a href="#11-ansible-playbook-中的-handlers" class="headerlink" title="11.ansible playbook 中的 handlers"></a>11.ansible playbook 中的 handlers</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行task之后，服务器发生变化之后要执行的一些操作，比如我们修改了配置文件后，需要重启一下服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/handlers.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- name: handlers <span class="built_in">test</span></div><div class="line">  hosts: 192.168.0.81</div><div class="line">  user: root</div><div class="line">  tasks:</div><div class="line">    - name: copy file</div><div class="line">    copy: src=/etc/passwd dest=/tmp/aaa.txt</div><div class="line">    notify: <span class="built_in">test</span> handlers</div><div class="line">  handlers:</div><div class="line">     - name: <span class="built_in">test</span> handlers</div><div class="line">       shell: <span class="built_in">echo</span> <span class="string">"111111"</span> &gt;&gt; /tmp/aaa.txt</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/36.png?raw=true" alt="36"><figcaption class="figure__caption">36</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明，只有copy模块真正执行后，才会去调用下面的handlers相关的操作。也就是说如果1.txt和2.txt内容是一样的，并不会去执行handlers里面的shell相关命令。 这种比较适合配置文件发生更改后，重启服务的操作。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible-playbook /etc/ansible/handlers.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端查看</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/37.png?raw=true" alt="37"><figcaption class="figure__caption">37</figcaption></figure></p>
<h2 id="12-ansible-安装-nginx"><a href="#12-ansible-安装-nginx" class="headerlink" title="12.ansible 安装 nginx"></a>12.ansible 安装 nginx</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;思路：先在一台机器上编译安装好nginx、打包，然后再用ansible去下发</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;进入ansible配置文件目录</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># cd /etc/ansible</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建一个nginx_install的目录，方便管理</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ansible]<span class="comment"># mkdir nginx_install</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ansible]<span class="comment"># cd nginx_install/</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible nginx_install]<span class="comment"># mkdir -p roles/&#123;common,install&#125;/&#123;handlers,files,meta,tasks,templates,vars&#125;</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：roles目录下有两个角色，common为一些准备操作，install为安装nginx的操作。每个角色下面又有几个目录，handlers下面是当发生改变时要执行的操作，通常用在配置文件发生改变，重启服务。files为安装时用到的一些文件，meta为说明信息，说明角色依赖等信息，tasks里面是核心的配置文件，templates通常存一些配置文件，启动脚本等模板文件，vars下为定义的变量</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;需要事先准备好安装用到的文件，具体如下：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在一台机器上事先编译安装好nginx，配置好启动脚本，配置好配置文件</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装好后，我们需要把nginx目录打包</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># cd /usr/local</span></div><div class="line">[root@ansible <span class="built_in">local</span>]<span class="comment"># tar zcvf nginx.tar.gz nginx</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;并放到/etc/ansible/nginx_install/roles/install/files/下面，名字为nginx.tar.gz</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible nginx_install]<span class="comment"># mv /usr/local/nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动脚本、配置文件都要放/etc/ansible/nginx_install/roles/install/templates下面</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@ansible roles]<span class="comment"># cd /etc/ansible/nginx_install/roles/install</span></div><div class="line">[root@ansible install]<span class="comment"># cp /usr/local/nginx/conf/nginx.conf templates/</span></div><div class="line">[root@ansible install]<span class="comment"># cp /etc/init.d/nginx templates/</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;定义common的tasks，nginx是需要一些依赖包的</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible nginx_install]<span class="comment"># cd /etc/ansible/nginx_install/roles</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible roles]<span class="comment"># vim ./common/tasks/main.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- name: Install initializtion require software</div><div class="line">  yum: name=&#123;&#123; item &#125;&#125; state=installed</div><div class="line">  with_items:</div><div class="line">    - zlib-devel</div><div class="line">    - pcre-devel</div><div class="line">    - openssl-devel</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/38.png?raw=true" alt="38"><figcaption class="figure__caption">38</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;定义变量</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible install]<span class="comment"># vim /etc/ansible/nginx_install/roles/install/vars/main.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">nginx_user: www</div><div class="line">nginx_port: 80</div><div class="line">nginx_basedir: /usr/<span class="built_in">local</span>/nginx</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/39.png?raw=true" alt="39"><figcaption class="figure__caption">39</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先要把所有用到的文档拷贝到目标机器</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible install]<span class="comment"># vim /etc/ansible/nginx_install/roles/install/tasks/copy.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">- name: Copy Nginx Software </div><div class="line">  copy: src=nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root</div><div class="line">- name: Uncompression Nginx Software</div><div class="line">  shell: tar zxf /tmp/nginx.tar.gz -C /usr/<span class="built_in">local</span>/</div><div class="line">- name: Copy Nginx Start Script</div><div class="line">  template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755</div><div class="line">- name: Copy Nginx Config</div><div class="line">  template: src=nginx.conf dest=&#123;&#123; nginx_basedir &#125;&#125;/conf/ owner=root group=root mode=0644</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/40.png?raw=true" alt="40"><figcaption class="figure__caption">40</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;接下来会建立用户，启动服务，删除压缩包</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/nginx_install/roles/install/tasks/install.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">- name: Create Nginx User</div><div class="line">  user: name=&#123;&#123; nginx_user &#125;&#125; state=present createhome=no shell=/sbin/nologin</div><div class="line">- name: Start Nginx Service</div><div class="line">  service: name=nginx state=restarted</div><div class="line">- name: Add Boot Start Nginx Service</div><div class="line">  shell: chkconfig --level 345 nginx on</div><div class="line">- name: Delete Nginx compression files</div><div class="line">  shell: rm -rf /tmp/nginx.tar.gz</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/41.png?raw=true" alt="41"><figcaption class="figure__caption">41</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再创建main.yml并且把copy和install调用</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/nginx_install/roles/install/tasks/main.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">- include: copy.yml</div><div class="line">- include: install.yml</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/42.png?raw=true" alt="42"><figcaption class="figure__caption">42</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;到此两个roles：common和install就定义完成了，接下来要定义一个入口配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/nginx_install/install.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- hosts: testhost</div><div class="line">  remote_user: root</div><div class="line">  gather_facts: True</div><div class="line">  roles:</div><div class="line">    - common</div><div class="line">    - install</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/43.png?raw=true" alt="43"><figcaption class="figure__caption">43</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行： ansible-playbook /etc/ansible/nginx_install/install.yml</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible-playbook /etc/ansible/nginx_install/install.yml</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/44.png?raw=true" alt="44"><figcaption class="figure__caption">44</figcaption></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/45.png?raw=true" alt="45"><figcaption class="figure__caption">45</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>注意：执行之前一定要检查所有机器 80 端口是否占用，否则会出错！</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端查看</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/46.png?raw=true" alt="46"><figcaption class="figure__caption">46</figcaption></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/47.png?raw=true" alt="47"><figcaption class="figure__caption">47</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载整个样例库   </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;git clone git://github.com/dl528888/ansible-examples.git</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># git clone git://github.com/dl528888/ansible-examples.git</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;git命令，需要yum先安装一下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># yum install -y git</span></div></pre></td></tr></table></figure>
<h2 id="13-ansible-管理配置文件"><a href="#13-ansible-管理配置文件" class="headerlink" title="13.ansible 管理配置文件"></a>13.ansible 管理配置文件</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;生产环境中大多时候是需要管理配置文件的，安装软件包只是在初始化环境的时候用一下。下面我们来写个管理nginx配置文件的playbook</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># mkdir -p /etc/ansible/nginx_config/roles/&#123;new,old&#125;/&#123;files,handlers,vars,tasks&#125;</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中new为更新时用到的，old为回滚时用到的，files下面为nginx.conf和vhosts目录，handlers为重启nginx服务的命令<br>关于回滚，需要在执行playbook之前先备份一下旧的配置，所以对于老配置文件的管理一定要严格，千万不能随便去修改线上机器的配置，并且要保证new/files下面的配置和线上的配置一致</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;先把nginx.conf和vhosts目录放到files目录下面</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># cp /usr/local/nginx/conf/nginx.conf /etc/ansible/nginx_config/roles/new/files/</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># cp -r /usr/local/nginx/conf/vhosts /etc/ansible/nginx_config/roles/new/files/</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/nginx_config/roles/new/vars/main.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;定义变量</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">nginx_basedir: /usr/<span class="built_in">local</span>/nginx</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/48.png?raw=true" alt="48"><figcaption class="figure__caption">48</figcaption></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/nginx_config/roles/new/handlers/main.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;定义重新加载nginx服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">- name: restart nginx</div><div class="line">  shell: /etc/init.d/nginx reload</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/49.png?raw=true" alt="49"><figcaption class="figure__caption">49</figcaption></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/nginx_config/roles/new/tasks/main.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这是核心的任务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">- name: copy conf file</div><div class="line"> copy: src=&#123;&#123; item.src &#125;&#125; dest=&#123;&#123; nginx_basedir &#125;&#125;/&#123;&#123; item.dest &#125;&#125; backup=yes owner=root group=root mode=0644</div><div class="line"> with_items:</div><div class="line"> - &#123; src: nginx.conf, dest: conf/nginx.conf &#125;</div><div class="line"> - &#123; src: vhosts, dest: conf/ &#125;</div><div class="line"> notify: restart nginx</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/50.png?raw=true" alt="50"><figcaption class="figure__caption">50</figcaption></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># vim /etc/ansible/nginx_config//update.yml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后是定义总入口配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">---</div><div class="line">- hosts: testhost</div><div class="line">  user: root</div><div class="line">  roles:</div><div class="line">    - new</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/51.png?raw=true" alt="51"><figcaption class="figure__caption">51</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行： </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible-playbook /etc/ansible/nginx_config/update.yml</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/52.png?raw=true" alt="52"><figcaption class="figure__caption">52</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;而回滚的backup.yml对应的roles为old    </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># rsync -av /etc/ansible/nginx_config/roles/new/ /etc/ansible/nginx_config/roles/old/</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@ansible ~]<span class="comment"># ansible-playbook /etc/ansible/nginx_config/backup.yml</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Ansible%20%E5%AE%89%E8%A3%85%E4%B8%8E%E9%85%8D%E7%BD%AE/53.png?raw=true" alt="53"><figcaption class="figure__caption">53</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;回滚操作就是把旧的配置覆盖，然后重新加载nginx服务</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/2. Linux 命令/9. Linux 命令- cp/">Linux 命令- cp</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cp命令用来复制文件或者目录，是Linux系统中最常用的命令之一。一般情况下，shell会设置一个别名，在命令行下复制文件时，如果目标文件已经存在，就会询问是否覆盖，不管你是否使用-i参数。但是如果是在shell脚本中执行cp时，没有-i参数时不会询问是否覆盖。这说明命令行和shell脚本的执行方式有些不同。 </p>
<h2 id="1．命令格式："><a href="#1．命令格式：" class="headerlink" title="1．命令格式："></a>1．命令格式：</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>用法：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp [选项] [-T] [源] [目的]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp [选项] [源] [目录]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp [选项] [-t] [目录]  [源]</div></pre></td></tr></table></figure>
<h2 id="2．命令功能："><a href="#2．命令功能：" class="headerlink" title="2．命令功能："></a>2．命令功能：</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将源文件复制至目标文件，或将多个源文件复制至目标目录。</p>
<h2 id="3．命令参数："><a href="#3．命令参数：" class="headerlink" title="3．命令参数："></a>3．命令参数：</h2><ul>
<li>-a, –archive    等于-dR –preserve=all    <ul>
<li>–backup[=CONTROL  为每个已存在的目标文件创建备份</li>
</ul>
</li>
<li>-b  类似–backup 但不接受参数     <ul>
<li>–copy-contents   在递归处理是复制特殊文件内容</li>
</ul>
</li>
<li>-d  等于–no-dereference –preserve=links</li>
<li>-f, –force 如果目标文件无法打开则将其移除并重试(当 -n 选项  存在时则不需再选此项)</li>
<li>-i, –interactive   覆盖前询问(使前面的 -n 选项失效)</li>
<li>-H   跟随源文件中的命令行符号链接</li>
<li>-l –link 链接文件而不复制</li>
<li>-L –dereference   总是跟随符号链接</li>
<li>-n –no-clobber   不要覆盖已存在的文件(使前面的 -i 选项失效)</li>
<li>-P –no-dereference   不跟随源文件中的符号链接</li>
<li>-p   等于–preserve=模式,所有权,时间戳    <ul>
<li>–preserve[=属性列表   保持指定的属性(默认：模式,所有权,时间戳)，如果可能保持附加属性：环境、链接、xattr 等</li>
</ul>
</li>
<li>-R, -r, –recursive  复制目录及目录内的所有项目</li>
</ul>
<h2 id="4．命令实例："><a href="#4．命令实例：" class="headerlink" title="4．命令实例："></a>4．命令实例：</h2><h3 id="实例1：复制单个文件到目标目录，文件在目标文件中不存在"><a href="#实例1：复制单个文件到目标目录，文件在目标文件中不存在" class="headerlink" title="实例1：复制单个文件到目标目录，文件在目标文件中不存在"></a>实例1：复制单个文件到目标目录，文件在目标文件中不存在</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp log.log test5</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cp log.log test5</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root    0 10-28 14:48 log.log</div><div class="line">drwxr-xr-x 6 root root 4096 10-27 01:58 scf</div><div class="line">drwxrwxrwx 2 root root 4096 10-28 14:47 test3</div><div class="line">drwxr-xr-x 2 root root 4096 10-28 14:53 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test5</span></div><div class="line">[root@localhost test5]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:53 log.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在没有带-a参数时，两个文件的时间是不一样的。在带了-a参数时，两个文件的时间是一致的。</p>
<h3 id="实例2：目标文件存在时，会询问是否覆盖"><a href="#实例2：目标文件存在时，会询问是否覆盖" class="headerlink" title="实例2：目标文件存在时，会询问是否覆盖"></a>实例2：目标文件存在时，会询问是否覆盖</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp log.log test5</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cp log.log test5</span></div><div class="line">cp：是否覆盖“test5/log.log”? n</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cp -a log.log test5</span></div><div class="line">cp：是否覆盖“test5/log.log”? y</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test5/</span></div><div class="line">[root@localhost test5]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:48 log.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;目标文件存在时，会询问是否覆盖。这是因为cp是cp -i的别名。目标文件存在时，即使加了-f标志，也还会询问是否覆盖。 </p>
<h3 id="实例3：复制整个目录"><a href="#实例3：复制整个目录" class="headerlink" title="实例3：复制整个目录"></a>实例3：复制整个目录</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp -a test3 test5</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;目标目录存在时：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cp -a test3 test5 </span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root    0 10-28 14:48 log.log</div><div class="line">drwxr-xr-x 6 root root 4096 10-27 01:58 scf</div><div class="line">drwxrwxrwx 2 root root 4096 10-28 14:47 test3</div><div class="line">drwxr-xr-x 3 root root 4096 10-28 15:11 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test5/</span></div><div class="line">[root@localhost test5]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root    0 10-28 14:46 log5-1.log</div><div class="line">-rw-r--r-- 1 root root    0 10-28 14:46 log5-2.log</div><div class="line">-rw-r--r-- 1 root root    0 10-28 14:46 log5-3.log</div><div class="line">-rw-r--r-- 1 root root    0 10-28 14:48 log.log</div><div class="line">drwxrwxrwx 2 root root 4096 10-28 14:47 test3</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;目标目录不存在是：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cp -a test3 test4</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">-rw-r--r-- 1 root root    0 10-28 14:48 log.log</div><div class="line">drwxr-xr-x 6 root root 4096 10-27 01:58 scf</div><div class="line">drwxrwxrwx 2 root root 4096 10-28 14:47 test3</div><div class="line">drwxrwxrwx 2 root root 4096 10-28 14:47 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-28 15:11 test5</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意目标目录存在与否结果是不一样的。目标目录存在时，整个源目录被复制到目标目录里面    </p>
<h3 id="实例4：复制的-log-log-建立一个连结档-log-link-log"><a href="#实例4：复制的-log-log-建立一个连结档-log-link-log" class="headerlink" title="实例4：复制的 log.log 建立一个连结档 log_link.log"></a>实例4：复制的 log.log 建立一个连结档 log_link.log</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp -s log.log log_link.log</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出：</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cp -s log.log log_link.log</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">lrwxrwxrwx 1 root root    7 10-28 15:18 log_link.log -&gt; log.log</div><div class="line">-rw-r--r-- 1 root root    0 10-28 14:48 log.log</div><div class="line">drwxr-xr-x 6 root root 4096 10-27 01:58 scf</div><div class="line">drwxrwxrwx 2 root root 4096 10-28 14:47 test3</div><div class="line">drwxrwxrwx 2 root root 4096 10-28 14:47 test4</div><div class="line">drwxr-xr-x 3 root root 4096 10-28 15:11 test5</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;那个 log_link.log 是由 -s 的参数造成的，建立的是一个『快捷方式』，所以您会看到在文件的最右边，会显示这个文件是『连结』到哪里去的！</p>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/48">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
